﻿<Window x:Class="WishfulCode.EC2RDP.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
        xmlns:c="clr-namespace:WishfulCode.EC2RDP.Controls"
        Height="600" Width="800"
        Background="Transparent"
        Icon="CloudConnect-icon.ico"
        Title="Cloud Terminal"
        >


    <Grid  DataContext="{Binding Source={StaticResource Locator}, Path=ConnectionList}">

        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="150" Width="195"></ColumnDefinition>
            <ColumnDefinition Width="3"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        
        <GridSplitter Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" />

        <Border x:Name="ConnectionBar" Background="White" Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch" Height="Auto">
        <DockPanel Background="White"  Margin="10" Width="Auto" VerticalAlignment="Stretch" Height="Auto">
            <ToolBar DockPanel.Dock="Top" Grid.Row="0">
              
                    <Button Click="AccountSettings_Click">
                        <TextBlock Text="Account..." />
                    </Button>
                    
                    <Button Command="{Binding RefreshWatchData}">
                        <TextBlock Text="Refresh..."></TextBlock>
                    </Button>

                    <Button Command="{Binding RunCloudCommand}">
                        <TextBlock Text="Run..."></TextBlock>
                    </Button>




                </ToolBar>
            <StatusBar DockPanel.Dock="Bottom">
                <StatusBarItem>
                    <TextBlock Text="{Binding InstanceDataStatus}" />
                </StatusBarItem>
            </StatusBar>
            <TextBlock DockPanel.Dock="Top" Margin="0 0 0 5" Text="Open Connections"></TextBlock>
            <ListBox DockPanel.Dock="Top"
                     MinHeight="40" Background="Red"
                     Grid.Column="0" GotFocus="openConnectionList_GotFocus" SelectionChanged="openConnectionList_SelectionChanged" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" x:Name="openConnectionList" ItemsSource="{Binding OpenConnections,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    <ListBox.ContextMenu>
                        <ContextMenu />
                    </ListBox.ContextMenu>
                    <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                        <EventSetter Event="MouseDoubleClick" Handler="ConnectionListItem_DoubleClick"/>
                    </Style>
                </ListBox.ItemContainerStyle>

                <ListBox.Template>
                    <ControlTemplate>
                        <Grid Background="White">
                            <ScrollViewer HorizontalScrollBarVisibility="Disabled">
                                <ItemsPresenter />
                            </ScrollViewer>
                        </Grid>
                    </ControlTemplate>
                </ListBox.Template>

                <ListBox.ItemTemplate>

                    <DataTemplate>
                        <DockPanel HorizontalAlignment="Stretch" Margin="0 0 0 4" VerticalAlignment="Stretch">
                            <Image Margin="5 0 4 0" DockPanel.Dock="Right" Width="60" Height="38" Source="{Binding StatusImage}" />
                            <StackPanel DockPanel.Dock="Left" Grid.Column="0" HorizontalAlignment="Left"  Orientation="Vertical">
                                <Grid DockPanel.Dock="Left">
                                    <TextBlock FontFamily="Segoe UI" FontWeight="Bold" Text="{Binding Name}" />
                                </Grid>
                                <Grid>
                                    <TextBlock FontFamily="Segoe UI" Text="{Binding Id}" />
                                </Grid>
                                <Grid>
                                    <TextBlock FontFamily="Segoe UI" Text="{Binding HexIp}" />
                                </Grid>
                                <Button Command="{Binding DisconnectCommand}" Content="X" />
                            </StackPanel>
                        </DockPanel>
                    </DataTemplate>
                    
                </ListBox.ItemTemplate>
            </ListBox>

            <TextBlock DockPanel.Dock="Top" Margin="0 8 0 5" Text="Available Connections"></TextBlock>
                <TextBlock DockPanel.Dock="Top"  Text="Filter :" ></TextBlock>
                <TextBox DockPanel.Dock="Top" Margin="0 0 0 5" Text="{Binding ClosedConnectionsFilter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></TextBox>
                <ListBox DockPanel.Dock="Top" Background="Red" Margin="6 0 0 0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" x:Name="connectionList" ItemsSource="{Binding ClosedConnections}" SelectionChanged="openConnectionList_SelectionChanged" GotFocus="openConnectionList_GotFocus">
                    <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                        <EventSetter Event="MouseDoubleClick" Handler="ConnectionListItem_DoubleClick"/>
                    </Style>
                </ListBox.ItemContainerStyle>

                <ListBox.Template>
                    <ControlTemplate>
                        <Grid Background="White">
                            <ScrollViewer HorizontalScrollBarVisibility="Disabled">
                                <ItemsPresenter />
                            </ScrollViewer>
                        </Grid>
                    </ControlTemplate>
                </ListBox.Template>

                <ListBox.ItemTemplate>

                    <DataTemplate>
                        <DockPanel HorizontalAlignment="Stretch" Margin="0 0 0 4" VerticalAlignment="Stretch">                           
                            <Image Margin="5 0 4 0" DockPanel.Dock="Right" Width="60" Height="38" Source="{Binding StatusImage}" />
                            <StackPanel DockPanel.Dock="Left" Grid.Column="0" HorizontalAlignment="Left"  Orientation="Vertical">
                                <Grid DockPanel.Dock="Left">
                                    <TextBlock FontFamily="Segoe UI" FontWeight="Bold" Text="{Binding Name}" />
                                </Grid>
                                <Grid>
                                    <TextBlock FontFamily="Segoe UI" Text="{Binding Id}" Margin="0 0 0 3"  />
                                </Grid>
                                <Grid>
                                        <TextBlock FontFamily="Segoe UI" Text="{Binding HexIp}" Margin="0 0 0 3" />
                                </Grid>
                            </StackPanel>
                            <DockPanel.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Copy Hostname to clipboard" Command="{Binding CopyHostnameToClipboard}" />
                                </ContextMenu>
                            </DockPanel.ContextMenu>

                        </DockPanel>
                    </DataTemplate>
                    
                </ListBox.ItemTemplate>
            </ListBox>

            

        </DockPanel>
</Border>
        


        <Border Grid.Row="0" BorderThickness="5" Grid.Column="2" BorderBrush="Transparent">
        <ListView x:Name="ConnectionViews" ItemsSource="{Binding Connections, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <ListView.Resources>
                    
                    <DataTemplate x:Key="ConnectionItem_Disconnected">
                        <Border Background="White" Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                            <StackPanel Orientation="Vertical">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock FontSize="24" FontWeight="Bold" Text="{Binding Name}" />
                                    <TextBlock FontSize="24" FontStyle="Italic" Margin="5 0 0 0" Text="("><Run Text="{Binding Id}" /><Run Text=")" /></TextBlock>
                                    
                                </StackPanel>
                                <TextBlock FontStyle="Italic" Text="Disconnected" Margin="0 2 0 10"></TextBlock>
                                <Button Command="{Binding ConnectCommand}">
                                    <TextBlock Text="Connect" />
                                </Button>
                                
                                <CheckBox IsChecked="{Binding ConnectWithMappedDrives}">
                                    <TextBlock Text="Connect with Mapped Drives?" />
                                </CheckBox>
                                <CheckBox IsEnabled="{Binding HavePrivateKeyStored}" IsChecked="{Binding UseApiAdminPwd}">
                                    <TextBlock Text="Fetch Windows Administrator Password from EC2 API?">
                                        
                                    <TextBlock FontStyle="Italic" FontWeight="Bold" Visibility="{Binding Path=HavePrivateKeyStored, Converter={StaticResource ReverseBooleanToVisibilityConverter}}" Text=" No valid private key!" />
</TextBlock>
                                </CheckBox>
                                <StackPanel Margin="0 10" Orientation="Horizontal">
                                    <TextBlock Text="The Private Key file '"><Run Text="{Binding KeyName}" /><Run Text="'" /></TextBlock>
                                    
                                    <TextBlock FontStyle="Italic" Visibility="{Binding Path=HavePrivateKeyStored, Converter={StaticResource BooleanToVisibilityConverter}}" Text=" was found in your Account settings." />
                                    <TextBlock FontStyle="Italic" FontWeight="Bold" Visibility="{Binding Path=HavePrivateKeyStored, Converter={StaticResource ReverseBooleanToVisibilityConverter}}" Text=" was not found in your Account settings!" />
                                </StackPanel>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                    
                    <DataTemplate x:Key="ConnectionItem_Connecting">
                        <Border Background="White" Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                            <c:ConnectionView x:Name="Connection" DataContext="{Binding}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"></c:ConnectionView>
                        </Border>
                    </DataTemplate>
                    
                </ListView.Resources>
                    <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListViewItem}">
                                
                                <ContentPresenter  x:Name="Control"></ContentPresenter>
                              
                                <ControlTemplate.Triggers>
                                    <Trigger  Property="IsSelected" Value="True">
                                        <Setter TargetName="Control" Property="Visibility" Value="Visible"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="Control" Property="Visibility" Value="Collapsed"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                   
                </Style>
            </ListView.ItemContainerStyle>
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        
                            <Grid></Grid>
                        
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                    <ListView.Template>
                <ControlTemplate>
                        <DockPanel>
                            <ItemsPresenter />
                        </DockPanel>
                    </ControlTemplate>
            </ListView.Template>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ContentControl Name="cc" Content="{Binding}" ContentTemplate="{StaticResource ConnectionItem_Disconnected}" />
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsDisconnected}" Value="True">
                                <Setter TargetName="cc" Property="ContentTemplate" Value="{StaticResource ConnectionItem_Disconnected}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsDisconnected}" Value="False">
                                <Setter TargetName="cc" Property="ContentTemplate" Value="{StaticResource ConnectionItem_Connecting}" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </Border>
        <Menu Height="193" HorizontalAlignment="Left" Margin="-195,459,0,0" Name="menu1" VerticalAlignment="Top" Width="200" IsMainMenu="False" />
    </Grid>
</Window>
